package aryn.robot;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import aryn.framework.utilities.Util;

public class Bot extends Robot {
	
	private String filePath;
	private int x;
	private int y;
	private int delayMin;	// delay between an action and another
	private int delayMax;
	
	public Bot() throws AWTException {
		filePath = null;
		delayMin = 300;
		delayMax = 500;
	}
	
	public void setDelay(int min, int max) {
		this.delayMin = min;
		this.delayMax = max;
	}
	
	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	protected void mouseClick(int x, int y, int button) {
		if(x != this.x || y != this.y)
			this.mouseMove(x, y);
		this.mousePress(button);
		this.mouseRelease(button);
		this.delay(Util.randomInt(delayMin, delayMax));
	}
	
	public void leftClick(int x, int y) {
		mouseClick(x, y, InputEvent.BUTTON1_DOWN_MASK);
	}
	
	public void leftClick() {
		leftClick(this.x, this.y);
	}
	
	public void rightClick(int x, int y) {
		mouseClick(x, y, InputEvent.BUTTON2_DOWN_MASK);
	}
	
	public void rightClick() {
		rightClick(this.x, this.y);
	}

	public void write(String s) {
		for(int i = 0; i < s.length(); i++)
			writeLetter(s.charAt(i));
	}
	
	private void writeLetter(char letter) {
		int value = (int)letter;
		boolean isUppercase = false;
		
		if(value >= 65 && value <= 90) // ASCII uppercase -> lowercase + true
			isUppercase = true;	
		if(value >= 97 && value <= 122) // ASCII lowercase -> lowercase
			value = value - 32;	
			
		System.out.println(value);
		if(isUppercase) this.keyPress(KeyEvent.VK_SHIFT);
		this.keyPress(value);
		this.delay(Util.randomInt(26, 57));
		this.keyRelease(value);
		if(isUppercase) this.keyRelease(KeyEvent.VK_SHIFT);
		this.delay(Util.randomInt(48, 63));
		if(isUppercase) this.delay(Util.randomInt(48, 113));
	}
	
	public void setScreenshotFilePath(String path) {
		this.filePath = path;
	}
	
	public void screenshot() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		BufferedImage screenshot = this.createScreenCapture(new Rectangle(dim.width, dim.height));
		filePath+="\\screenshot";
		File file = new File(filePath);
		if (!file.exists())
			file.mkdir();
		file = new File(filePath + "\\screenshot0.jpg");
		int counter = 0;
		while(file.exists()) {
			counter++;
			file = new File(filePath + "\\screenshot" + counter + ".jpg");
		}
		try {
			ImageIO.write(screenshot,
					"jpg", 
					file);
		} catch (IOException e) {
			System.out.println(filePath == null? "filePath is null!" : "Couldn't open the file: "+filePath);
			e.printStackTrace();
		}
		System.out.println("Screenshot done.");
	}
	
	
}
